package com.riddle.vmodel;

import java.util.ArrayList;
import java.util.List;

import com.riddle.model.Permission;
import com.riddle.model.TreeModel;

public class VTreeModel {

	private String id;
	
	private String text;
	
	private VTreeModel parent;
	
	private List<VTreeModel> children = new ArrayList<VTreeModel>();

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public VTreeModel getParent() {
		return parent;
	}

	public void setParent(VTreeModel parent) {
		this.parent = parent;
	}

	public List<VTreeModel> getChildren() {
		return children;
	}

	public void setChildren(List<VTreeModel> children) {
		this.children = children;
	}
	
	public static List<VTreeModel> toVTreeModel(List list){
		
		List<VTreeModel> res = new ArrayList<VTreeModel>();
		for(Object o : list) {
			TreeModel t = (TreeModel)o;
			VTreeModel model = new VTreeModel();
			model.setId(t.getId().toString());
			model.setText(t.getName());
			transverList(t.getChildren(), model);
			res.add(model);
		}
		
		return res;
	}
	
	private static void transverList(List<TreeModel> list, VTreeModel res) {
		if(res.getChildren() == null) {
			res.setChildren(new ArrayList<VTreeModel>());
		}
		for(Object o : list) {
			TreeModel s = (TreeModel)o;
			VTreeModel smodel = new VTreeModel();
			smodel.setId(s.getId().toString());
			smodel.setText(s.getName());
			transverList(s.getChildren(), smodel);
			res.getChildren().add(smodel);
		}
	}

}
